package org.darkforest.merchant.controller;

import org.darkforest.common.util.DateUtil;
import org.darkforest.common.services.TransportService;
import org.darkforest.model.Area;
import org.darkforest.model.Transport;
import org.darkforest.model.TransportExtendWithBLOBs;
import org.darkforest.model.TransportPageBean;
import org.darkforest.model.TransportVo;
import org.darkforest.ucenter.service.AreaRedisService;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by bingo on 17/1/30.
 * 订单物流相关控制类
 */
@Controller
@RequestMapping(value="/transport")
public class TransPortController {

    @Autowired
    private TransportService transportService;
    @Autowired
    private AreaRedisService areaRedisService;
    /**
     * 订单列表
     * @param request
     * @return
     */
    @RequestMapping(value = "transportList")
    public ModelAndView transportList(HttpServletRequest request,TransportPageBean page,String type ) {
        Integer storeId = ((StoreSellerVo) request.getSession().getAttribute("sellerInfo")).getStoreId();
        page.setStoreId(storeId);
        ModelAndView m =  new ModelAndView("transport/transportList", "page", page);
        transportService.selectPageTransport(page);
        String copyMessage = request.getParameter("copyMessage");
        m.addObject("copyMessage",copyMessage);
        m.addObject("type",type);
        return m;
    }


    @RequestMapping(value = "addTransport")
    public ModelAndView addTransport(HttpServletRequest request) {
       List<Area> areaList = transportService.createArea();
        List<Area> provinceList = transportService.getAreaList(0);
        for(Area a:areaList){
            for(Area p:provinceList){
                if(a.getAreaName().equals(p.getAreaRegion())){
                    List<Area> cityList = transportService.getAreaList(p.getAreaId());
                    p.setChildrenList(cityList);
                    a.getChildrenList().add(p);
                }
            }
        }
        return new ModelAndView("transport/addTransport","areaList",areaList);
    }


    @RequestMapping(value = "doAddTransport")
    public ModelAndView doAddTransport(HttpServletRequest request) {
        Integer storeId = ((StoreSellerVo) request.getSession().getAttribute("sellerInfo")).getStoreId();
        String title = request.getParameter("title");
        String defaultStart = request.getParameter("default[kd][start]");
        String defaultpostage =request.getParameter("default[kd][postage]");
        String defaultPlus =  request.getParameter("default[kd][plus]");
        String defaultPostageplus = request.getParameter("default[kd][postageplus]");

        Transport transport = new Transport();
        transport.setTitle(title);
        transport.setUpdateTime(DateUtil.unixTimeStampInt().intValue());
        transport.setStoreId(storeId);
        int x =  transportService.insertTransport(transport);

        TransportExtendWithBLOBs te = new TransportExtendWithBLOBs();
        te.setAreaName("全国");
        te.setIsDefault("1");
        te.setSnum(Integer.parseInt(defaultStart));
        te.setSprice(new BigDecimal(defaultpostage));
        te.setXnum(Integer.parseInt(defaultPlus));
        te.setXprice(new BigDecimal(defaultPostageplus));
        te.setTransportTitle(title);
        te.setTransportId(transport.getId());
        int y = transportService.insertTransportExtend(te);
        int tNum = request.getParameter("tNum")==""?0:Integer.parseInt(request.getParameter("tNum"));
        String data = "";
        String []datas= null;
        String ids = "";
        String names = "";
        for(int i = 1;i<=tNum;i++){
            data =       request.getParameter("areas[kd]["+i+"]");
            defaultStart =       request.getParameter("special[kd]["+i+"][start]");
            defaultpostage =     request.getParameter("special[kd]["+i+"][postage]");
            defaultPlus =       request.getParameter("special[kd]["+i+"][plus]");
            defaultPostageplus = request.getParameter("special[kd]["+i+"][postageplus]");
            if(data==null)
                continue;
            datas = data.split("\\|\\|\\|");
            if(datas==null)
                continue;
            ids = datas[0];
            names = datas[1];

            TransportExtendWithBLOBs tt = new TransportExtendWithBLOBs();
            tt.setAreaName(names);
            tt.setAreaId(ids);
            tt.setIsDefault("2");
            tt.setSnum(Integer.parseInt(defaultStart));
            tt.setSprice(new BigDecimal(defaultpostage));
            tt.setXnum(Integer.parseInt(defaultPlus));
            tt.setXprice(new BigDecimal(defaultPostageplus));
            tt.setTransportTitle(title);
            tt.setTransportId(transport.getId());
            y = transportService.insertTransportExtend(tt);
        }
        return new ModelAndView("redirect:/transport/transportList");
    }

    @RequestMapping(value = "updateTransport")
    public ModelAndView updateTransport(HttpServletRequest request,Transport transport,int start) {
        List<Area> areaList = transportService.createArea();
        List<Area> provinceList = transportService.getAreaList(0);
        for(Area a:areaList){
            for(Area p:provinceList){
                if(a.getAreaName().equals(p.getAreaRegion())){
                    List<Area> cityList = transportService.getAreaList(p.getAreaId());
                    p.setChildrenList(cityList);
                    a.getChildrenList().add(p);
                }
            }
        }
        TransportVo tVo = transportService.getTransportVoById(transport.getId());
        ModelAndView m = new ModelAndView("transport/updateTransport","areaList",areaList);
        m.addObject("tVo",tVo);
        m.addObject("start",start);
        return m;
    }


    @RequestMapping(value = "doUpdateTransport")
    public ModelAndView doUpdateTransport(HttpServletRequest request,int id,int start) {
        Integer storeId = ((StoreSellerVo) request.getSession().getAttribute("sellerInfo")).getStoreId();

        String title = request.getParameter("title");
        String defaultStart = request.getParameter("default[kd][start]");
        String defaultpostage = request.getParameter("default[kd][postage]");
        String defaultPlus = request.getParameter("default[kd][plus]");
        String defaultPostageplus = request.getParameter("default[kd][postageplus]");

        Transport transport = new Transport();
        transport.setTitle(title);
        transport.setUpdateTime(DateUtil.unixTimeStampInt().intValue());
        transport.setStoreId(storeId);
        int x = transportService.insertTransport(transport);

        TransportExtendWithBLOBs te = new TransportExtendWithBLOBs();
        te.setAreaName("全国");
        te.setIsDefault("1");
        te.setSnum(Integer.parseInt(defaultStart));
        te.setSprice(new BigDecimal(defaultpostage));
        te.setXnum(Integer.parseInt(defaultPlus));
        te.setXprice(new BigDecimal(defaultPostageplus));
        te.setTransportTitle(title);
        te.setTransportId(transport.getId());
        int y = transportService.insertTransportExtend(te);
        int tNum = Integer.parseInt(request.getParameter("tNum"));
        String data = "";
        String[] datas = null;
        String ids = "";
        String names = "";
        for (int i = 1; i <= tNum; i++) {
            data = request.getParameter("areas[kd][" + i + "]");
            defaultStart = request.getParameter("special[kd][" + i + "][start]");
            defaultpostage = request.getParameter("special[kd][" + i + "][postage]");
            defaultPlus = request.getParameter("special[kd][" + i + "][plus]");
            defaultPostageplus = request.getParameter("special[kd][" + i + "][postageplus]");
            if (data == null)
                continue;
            datas = data.split("\\|\\|\\|");
            if (datas == null)
                continue;
            ids = datas[0];
            names = datas[1];

            TransportExtendWithBLOBs tt = new TransportExtendWithBLOBs();
            tt.setAreaName(names);
            tt.setAreaId(ids);
            tt.setIsDefault("2");
            tt.setSnum(Integer.parseInt(defaultStart));
            tt.setSprice(new BigDecimal(defaultpostage));
            tt.setXnum(Integer.parseInt(defaultPlus));
            tt.setXprice(new BigDecimal(defaultPostageplus));
            tt.setTransportTitle(title);
            tt.setTransportId(transport.getId());
            transportService.insertTransportExtend(tt);
        }
        if(id!=0){
            transportService.deleteTransportAndExtend(id);
        }
        return new ModelAndView("redirect:/transport/transportList?start="+start);
    }

    @RequestMapping(value = "deleteTransport")
    public ModelAndView deleteTransport(HttpServletRequest reques,Transport transport,int start) {
        transportService.deleteTransportAndExtend(transport.getId());
        return new ModelAndView("redirect:/transport/transportList?start="+start);
    }


    @RequestMapping(value = "copyTransport")
    public ModelAndView copyTransport(HttpServletRequest reques,Transport transport,int start) {
        int ret = transportService.copyTransport(transport.getId());
        return new ModelAndView("redirect:/transport/transportList?start="+start+"&copyMessage=true");
    }
}
